import 'package:get/get.dart';
import 'package:kkhousekeep/net/respnse_code.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';

import '../../../../models/home_visitor_record_detail_entity.dart';
import '../../../../net/home_visitor_connect.dart';

class RecordDetailController extends GetxController {
  int visitorId;

  RecordDetailController(this.visitorId);

  late HomeVisitorConnect connect;
  var hadLoadFinished = false.obs;
  late HomeVisitorRecordDetailObjInfo info;

  @override
  void onInit() {
    super.onInit();
    connect = Get.find<HomeVisitorConnect>();
    loadData(visitorId);
  }

  Future<void> loadData(int visitorId) async {
    var response = await connect.getVisitorDetail(visitorId: visitorId);
    if (response == null) {
      return;
    }
    if (response.code == ResponseCode.success) {
      info = response.obj.info;
      _processInfoLength();
      hadLoadFinished.value = true;
      return;
    }
    DialogUtil.showNetworkFail();
  }

  void _processInfoLength(){
    if(info.visitor.length >10){
      info.visitor = info.visitor.replaceRange(10, info.visitor.length, "...");
    }
    if(info.address.length > 10){
     info.address =  info.address.replaceRange(10, info.address.length, "...");
    }
  }

}
